package com.example.carrierapp.ui.main.waybill.arriveno

import android.content.Context

import com.example.carrierapp.base.BasePresenter
import com.example.carrierapp.bean.RowsBean
import com.example.carrierapp.net.ApiMiddleHelper
import com.example.carrierapp.net.listener.DataCallBackListener
import com.example.carrierapp.util.LogUtil
import com.example.carrierapp.viewmodel.itemmodel.ItemWaybillModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

import java.util.HashMap

class WaybillArriveNoPresenter(private val mContext: Context) : BasePresenter<WaybillArriveNoContract.View>(), WaybillArriveNoContract.Presenter {

    override fun start() {

    }

    override fun getWaybillList(skipCount: Int, filter: String) {
        val biddingMap = HashMap<String, String>()
        biddingMap["billState"] = "2"
        biddingMap["maxResultCount"] = "10"
        biddingMap["skipCount"] = skipCount.toString()
        biddingMap["filter"] = filter
        val flowable = apiStrores.getWaybillList(biddingMap)

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("getWaybillList-onSuccess-$json")

                val bean = Gson().fromJson<RowsBean<ItemWaybillModel>>(json, object : TypeToken<RowsBean<ItemWaybillModel>>() {}.type)
                if (view != null) {
                    view.getWaybillListSuccess(bean)

                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("getWaybillList-onFailure-$msg")
                if (view != null) {
                    view.getWaybillListFailde(msg)

                }
            }
        })
    }

    override fun modifyVolume(id: Int,carNum: String, isConfirmed: Boolean) {
        val biddingMap = HashMap<String, String>()
        biddingMap["carNum"] = carNum + ""
        biddingMap["id"] = id.toString() + ""
        biddingMap["isConfirmed"] = isConfirmed.toString()
        val flowable = apiStrores.modifyCarnumOrVolume(biddingMap)

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("modifyVolume-onSuccess-$json")
                if (view != null) {
                    view.modifyVolumeSuccess(json)
                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("modifyVolume-onFailure-$msg")
                if (view != null) {

                    view.modifyVolumeFailde(msg)

                }
            }
        })
    }

    override fun cancelWaybill(id: Int) {
        val biddingMap = HashMap<String, String>()
        biddingMap["id"] = id.toString() + ""
        val flowable = apiStrores.cancelWaybill(biddingMap)

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("cancelWaybill-onSuccess-$json")
                if (view != null) {
                    view.cancelWaybillSuccess(json)

                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("cancelWaybill-onFailure-$msg")
                if (view != null) {
                    view.cancelWaybillFailde(msg)
                }
            }
        })
    }

}
